//
//  UILabel+XLExtension.m
//  DrugProphet
//
//  Created by 左学良 on 16/8/10.
//  Copyright © 2016年 左学良. All rights reserved.
//

#import "UILabel+XLExtension.h"

@implementation UILabel (XLExtension)

- (void)setLabelSpaceWithValue:(NSString*)str withFont:(UIFont*)font alignment:(NSTextAlignment)alignment{
    
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
    paraStyle.lineBreakMode = NSLineBreakByWordWrapping;
    paraStyle.alignment = alignment;
    paraStyle.lineSpacing = 5; // 行间距
    paraStyle.hyphenationFactor = 1.0;  //连字符属性
    paraStyle.firstLineHeadIndent = 30; // 首行缩进50px
    paraStyle.paragraphSpacingBefore = 10; // 段首空间20px
    paraStyle.headIndent = 0; // 头部缩进20px
    paraStyle.tailIndent = 0; // 尾部缩进20px
    // set the word spacing NSKernAttributeName:1.5f
    NSDictionary *dic = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@1.0f
                          };
    NSAttributedString *attributeStr = [[NSAttributedString alloc] initWithString:str attributes:dic];
    self.attributedText = attributeStr;
}

// calculate the height of the label With the line spacing

+ (CGFloat)getSpaceLabelHeight:(NSString*)str withFont:(UIFont*)font withWidth:(CGFloat)width alignment:(NSTextAlignment)alignment{
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
    paraStyle.lineBreakMode = NSLineBreakByWordWrapping;
    paraStyle.alignment = alignment;
    paraStyle.lineSpacing = 5; // 行间距
    paraStyle.hyphenationFactor = 1.0;  //连字符属性
    paraStyle.firstLineHeadIndent = 50; // 首行缩进50px
    paraStyle.paragraphSpacingBefore = 10; // 段首空间20px
    paraStyle.headIndent = 10; // 头部缩进20px
    paraStyle.tailIndent = -10; // 尾部缩进20px
    // set the word spacing NSKernAttributeName:1.5f
    NSDictionary *dic = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@1.0f
                          };
    CGSize size = [str boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;
    return size.height;
}
@end
